package com.aixingfu.sports.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aixingfu.sports.Adapter.MemberListAdapter;
import com.aixingfu.sports.MVP.contract.SearchContract;
import com.aixingfu.sports.MVP.presenter.SearchPresenter;
import com.aixingfu.sports.Model.SearchModel;
import com.aixingfu.sports.R;
import com.aixingfu.sports.view.SpaceItemDecoration;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orie.librarybase.base.BaseActivity;
import com.orie.librarybase.utils.UserUtils;
import com.vondear.rxtool.view.RxToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//会员列表
@Route(path = "/main/MemberListActivity")
public class MemberListActivity extends BaseActivity implements SearchContract.MyView {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycle_member)
    RecyclerView recycleMember;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.mSwipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private List<SearchModel.DataBean> searchList = new ArrayList<>();
    private MemberListAdapter memberListAdapter;
    private int page = 1;
    private int number = 10;
    private SearchPresenter searchPresenter;
    private String keywords;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_member_list;
    }

    public static void startActivityByRoute() {
        ARouter.getInstance().build("/main/MemberListActivity").navigation();
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        tvTitle.setText("会员列表");
        Bundle bundle = getIntent().getExtras();
        keywords = bundle.getString("keywords", "");
//        for (int i = 0; i < 6; i++) {
//            stringList.add("");
//        }
//        recycleMember.setLayoutManager(new LinearLayoutManager(MemberListActivity.this, LinearLayoutManager.VERTICAL, false));
//        memberListAdapter = new MemberListAdapter(MemberListActivity.this, R.layout.item_member, stringList);
//        recycleMember.setAdapter(memberListAdapter);

        // 设置颜色属性的时候一定要注意是引用了资源文件还是直接设置16进制的颜色，因为都是int值容易搞混
        // 设置下拉进度的背景颜色，默认就是白色的
        mSwipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        mSwipeRefreshLayout.setColorSchemeResources(R.color.textColor5, R.color.purple, R.color.yellow1);
        mSwipeRefreshLayout.setRefreshing(true);
        searchPresenter = new SearchPresenter();
        searchPresenter.attachView(this);
        getSearch(1);
        recycleMember.setLayoutManager(new LinearLayoutManager(MemberListActivity.this, LinearLayoutManager.VERTICAL, false));
        memberListAdapter = new MemberListAdapter(MemberListActivity.this, R.layout.item_member, searchList);
        recycleMember.addItemDecoration(new SpaceItemDecoration(0, 20));
//        //设置分割线
//        recycleList.addItemDecoration(new DividerItemDecoration(
//                getActivity(), DividerItemDecoration.VERTICAL));
        recycleMember.setAdapter(memberListAdapter);
        memberListAdapter.setEmptyView(LayoutInflater.from(MemberListActivity.this).inflate(R.layout.empty_data, null, false));
        memberListAdapter.disableLoadMoreIfNotFullPage(recycleMember);
        memberListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                recycleMember.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        Log.e(TAG, "run==: 来了来了");
                        if (page * number == searchList.size()) {//还有数据
                            Log.e(TAG, "run==: 加载完这一次");
                            page = page + 1;
                            getSearch(2);
                        } else {//加载完了
                            memberListAdapter.loadMoreEnd();
                        }
                    }
                }, 100);
            }
        });
        // 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // 开始刷新，设置当前为刷新状态
                if (mSwipeRefreshLayout != null) {
                    mSwipeRefreshLayout.setRefreshing(false);
                }
                page = 1;
                if (searchList != null){
                    searchList.clear();
                }
                getSearch(1);
                // 加载完数据设置为不刷新状态，将下拉进度收起来
//                swipeRefreshView.setRefreshing(false);
            }
        });
        memberListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String morganization = UserUtils.getOrganization(MemberListActivity.this);
                if ("xiaoshou".equals(morganization)){
                    ARouter.getInstance().build("/main/SelectCardTypeActivity").withString("id",searchList.get(position).getMember_id()).navigation();
                }else if ("sijiao".equals(morganization)){
                    RxToast.normal(MemberListActivity.this,"私教不能续费升级！").show();
                }

            }
        });
        memberListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                String organization = UserUtils.getOrganization(MemberListActivity.this);
                int id = view.getId();
                if (id == R.id.tv_buy_class) {
                    if ("sijiao".equals(organization)){
                        ARouter.getInstance().build("/main/BuyingPrivateCoursesActivity").withString("mobile",searchList.get(position).getMobile()).withString("member_id",searchList.get(position).getMember_id()).navigation();
                    }else if ("xiaoshou".equals(organization)){
                        RxToast.normal(MemberListActivity.this,"销售不能出售私教课！").show();
                    }
                }else if (id == R.id.tv_buy_card){
                    if ("xiaoshou".equals(organization)){
                        ARouter.getInstance().build("/main/ElectronicContractActivity")
                                .withString("userName",searchList.get(position).getUsername())
                                .withString("sex",searchList.get(position).getSex())
                                .withString("mobile",searchList.get(position).getMobile())
                                .withString("cardtype",searchList.get(position).getCard_type())
                                .withString("idCard",searchList.get(position).getId_card())
                                .withString("member_id",searchList.get(position).getMember_id())
                                .withString("birth_data",searchList.get(position).getBirth_date())
                                .withString("type","1").navigation();
                    }else if ("sijiao".equals(organization)){
                        RxToast.normal(MemberListActivity.this, "只有销售才能购卡").show();
                    }
                }
            }
        });
    }

    @Override
    protected void initEvent() {
    }
    @Override
    protected void beforeInit() {
    }
    public void getSearch(int state){
        if (searchPresenter.isViewAttached()){
            HashMap<String,String> map = new HashMap<>();
            map.put("keywords",keywords);
            map.put("page", String.valueOf(page));
            map.put("per-page", String.valueOf(number));
            searchPresenter.getSerch(map,state);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showToast(String msg, String state) {

    }
    @Override
    public void setSearch(SearchModel data, int state) {
        if (data.getCode() == 1) {
            if (state == 1) {
                searchList = data.getData();
                memberListAdapter.setNewData(searchList);

                if (mSwipeRefreshLayout != null){
                    // 加载完数据设置为不刷新状态，将下拉进度收起来
                    mSwipeRefreshLayout.setRefreshing(false);
                }

            } else {
                searchList.addAll(data.getData());
                memberListAdapter.setNewData(searchList);
                if (mSwipeRefreshLayout != null){
                    // 加载完数据设置为不刷新状态，将下拉进度收起来
                    mSwipeRefreshLayout.setRefreshing(false);
                }

            }
        }else {
            if (mSwipeRefreshLayout != null){
                // 加载完数据设置为不刷新状态，将下拉进度收起来
                mSwipeRefreshLayout.setRefreshing(false);
            }
        }
    }


    @Override
    public void showLoading() {
showLoadingDialog();
    }

    @Override
    public void hideLoading() {
dismissLoadingDialog();
    }

    @Override
    public Context getMyContext() {
        return this;
    }
}
